/**
 * Created by Administrator on 2017/8/25 0025.
 */
showImage = function (value, row, index) {
    var img = row.waierPic;
    var imgUrl = "";
    if (img != null && img != '') {
        imgUrl = File_Download_Url + img;
        return [
            '<image src=' + imgUrl + ' style="width:80px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    } else {
        return [
            '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    }
};

operateFormatter = function () {
    return [

        '<a class="edit" href="javascript:void(0)" title="编辑">编辑分类</a>' +
        '&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" style="color: red">删除分类</a>'
    ].join('');
};

window.operateEvents = {
    'click .edit': function (e, value, row) {
        showModel("updateWaiterType-div", function () {
            $("#id").val(row.id);
            $("#typeName").val(row.waiterName);
            $("#sorting").val(row.sorting);
            $("#select").val(row.isSpecial);
            $("#updatePrice").val(row.price);
            var imgUrl = File_Download_Url;
            $("#imgs").attr("src", imgUrl + row.waierPic);
            $("#imgs").attr("img-path", row.waierPic);
        })
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/type/updateLifeState'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};


/**
 * 添加
 */
bindClick("#addWaiterType", function () {
    showModel("addWaiterType-div", function () {
        $("#dumpWaiterName").val('');
        $("#sortings").val('');
        $("#selectId").val(-1);
    })
});

/**
 * 添加按钮事件
 */
bindClick("button[data-event=submit-addWaiterType]", function (tag, typeId) {
    var images = $("#img").attr("img-path");
    if (images == "") {
        hint("请添加图片");
        return;
    }
    $("#waiterPic").val(images);

    // var select = $("#selectId").val();
    // $("#selectIds").val(select);

    var dumpWaiterName = $("#dumpWaiterName").val();
    if (dumpWaiterName != null & dumpWaiterName != "") {
        // if (select != null & select != "") {
            var form = $("#addWaiterTypeFrom");
            var param = form.formToArray();
            $.post($().AbsUrlParse('/web/type/addLifeType'), param, function (res) {
                if (res.state && res.res.code == 40000) {
                    hint('添加成功！');
                    //关闭model方法
                    closeModel('addWaiterType-div');
                    var table = $("#table");
                    table.bootstrapTable("refresh");
                } else {
                    if (res.state == false) {
                        hint(res.msg);
                    } else {
                        hint(res.res.msg);
                    }
                }
            });
        // } else {
        //     hint("请选择服务类型");
        // }
    } else {
        hint("请添加分类名称");
    }
});

/**
 * 修改按钮事件
 */
bindClick("button[data-event=submit-updateWaiterType]", function () {
    var ima = $("#imgs").attr("img-path");
    $("#waiterPicUpdate").val(ima);

    var name = $("#typeName").val();
    if (name == "") {
        hint("分类名称不能为空");
        return;
    }

    var select = $("#select").val();
    $("#selectIdUpdate").val(select);
    if (select != -1) {
        var form = $("#updateWaiterTypeFrom");
        var param = form.formToArray();
        $.post($().AbsUrlParse('/web/type/updateWaiterLifeType'), param, function (res) {
            if (res.state && res.res.code == 40000) {
                hint('修改成功！');
                //关闭model方法
                closeModel('updateWaiterType-div');
                var table = $("#table");
                table.bootstrapTable("refresh");
            } else {
                if (res.state == false) {
                    hint(res.msg);
                } else {
                    hint(res.res.msg);
                }
            }
        });
    } else {
        hint("请选择是否为特殊");
    }
});

function queryWaiterName() {
    var name = $("#waiterName").val();
    var url = $().AbsUrlParse("/web/type/queryLifeType?name=" + name);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

isHome = function (value, row, index) {
    if (row.isHome == 1) {
        return ['<a class="update" href="javascript:void(0)">是</a>'].join();
    } else {
        return ['<a class="update1" href="javascript:void(0)" style="color: red">否</a>'].join();
    }
};

window.operateIsHome = {
    'click .update': function (e, value, row) {
        comment.showOperationTips("确定要取消首页推荐吗？", function () {
            $.post($().AbsUrlParse('/web/type/updateIsHome'), {id: row.id, isHome: 0}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .update1': function (e, value, row) {
        comment.showOperationTips("确定要开启首页推荐吗？", function () {
            $.post($().AbsUrlParse('/web/type/updateIsHome'), {id: row.id, isHome: 1}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

sorting = function (value, row, index) {
    return "<a href='javascript:void(0)' onclick='updateSorting(" + row.sorting + "," + row.id + ")'><span>" + row.sorting + "</span></a>";
};

/**
 * 点击排名事件
 * @param orderNum
 * @param id
 */
var updateSorting = function (sorting, id) {
    showModel("showSorting-div", function () {
        $("#pid").val(id);
        $("#sorting").val(sorting);
    });
};

/**
 * 判断输入是否为数字
 * @param v
 * @returns {boolean}
 */
function isNumber(v) {
    var str = v.val();
    var reg = parseFloat(str);
    var re = /^\+?[0-9][0-9]*$/;
    if (!re.test(str)) {
        return false;
    }
    return !isNaN(reg);
}

/**
 * 修改轮播推荐排名按钮点击事件
 */
bindClick("button[data-event=merge-sorting]", function () {
    var form = $("#updateSortingFrom");
    if (!form.valid()) {
        return false;
    }
    var sorting = $("#sorting");
    if (!isNumber(sorting)) {
        hint("请填入非负的正整数.");
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse('/web/type/updateWaiterLifeType'), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('showSorting-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}